package com.tl.commonlibrary.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.tl.commonlibrary.R;

/**
 * Created by lisi on 2017/6/22.
 */

public class ProgressWebView extends WebView {

    private ProgressBar mProgressbar;


    MysetWebViewClient mysetWebViewClient;

    public MysetWebViewClient getMysetWebViewClient() {
        return mysetWebViewClient;
    }

    public void setMysetWebViewClient(MysetWebViewClient mysetWebViewClient) {
        this.mysetWebViewClient = mysetWebViewClient;
    }

    public interface MysetWebViewClient {
        void myonPageStarted(WebView view, String url, Bitmap favicon);
    }

    public ProgressWebView(Context context) {
        super(context);
        init(context);

    }

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public ProgressWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    private void init(Context context) {
        mProgressbar = new ProgressBar(context, null,
                android.R.attr.progressBarStyleHorizontal);
        mProgressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                5, 0, 0));

        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.progress_bar_states);

        mProgressbar.setProgressDrawable(drawable);
        addView(mProgressbar);

        setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (mysetWebViewClient != null) {
                    mysetWebViewClient.myonPageStarted(view, url, favicon);
                }
            }
        });


        setWebChromeClient(new WebChromeClient());
        //是否可以缩放
        getSettings().setSupportZoom(false);
        getSettings().setJavaScriptEnabled(true);
        getSettings().setAppCacheEnabled(false);//是否使用缓存
    }

    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                mProgressbar.setVisibility(GONE);
            } else {
                if (mProgressbar.getVisibility() == GONE)
                    mProgressbar.setVisibility(VISIBLE);
                mProgressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        LayoutParams lp = (LayoutParams) mProgressbar.getLayoutParams();
        lp.x = l;
        lp.y = t;
        mProgressbar.setLayoutParams(lp);
        super.onScrollChanged(l, t, oldl, oldt);
    }

}